package com.immo.libcommon.view;

import android.app.Dialog;

import android.content.Context;

import android.os.Bundle;

import android.view.Display;

import android.view.View;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.TextView;

import com.immo.libcommon.R;


/**

 * Created by YaoChen on 2016/12/26.

 */



public class MyDialog extends Dialog {

    private Button yes;//确定按钮

    private Button no;//取消按钮

    private TextView messageTv;//消息提示文本

    private String messageStr;//从外界设置的消息文本

    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器

    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    private boolean showYes = true, showNo = true;

    private Context context;

    private String content;



    public MyDialog setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {

        if (str != null) {

            noStr = str;

        }

        this.noOnclickListener = onNoOnclickListener;

        return this;

    }



    public MyDialog setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {

        if (str != null) {

            yesStr = str;

        }

        this.yesOnclickListener = onYesOnclickListener;

        return this;

    }

    public MyDialog(Context context) {

        super(context, R.style.MyDialog);

    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.custom_dialog_layout);

        setCanceledOnTouchOutside(true);

        initView();

        initData();

        initEvent();

        WindowManager windowManager = getWindow().getWindowManager();

        Display display = windowManager.getDefaultDisplay();

        WindowManager.LayoutParams lp = getWindow().getAttributes();

        lp.width = display.getWidth(); //设置宽度

        lp.height = display.getHeight();

        getWindow().setAttributes(lp);

    }



    public void setButtonVisible(boolean yesVisible, boolean cancelVisible) {

        this.showYes = yesVisible;

        this.showNo = cancelVisible;

        if (yes != null) {

            yes.setVisibility(showYes ? View.VISIBLE : View.GONE);

        }

        if (no != null) {

            no.setVisibility(showNo ? View.VISIBLE : View.GONE);

        }

    }



    private void initEvent() {

        yes.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                dismiss();

                if (yesOnclickListener != null) {

                    yesOnclickListener.onYesClick();



                }

            }

        });

        no.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                dismiss();

                if (noOnclickListener != null) {

                    noOnclickListener.onNoClick();



                }

            }

        });

    }



    private void initData() {



        if (messageStr != null) {

            messageTv.setText(messageStr);

        }

        if (yesStr != null) {

            yes.setText(yesStr);

        }

        if (noStr != null) {

            no.setText(noStr);

        }

    }

    private void initView() {

        yes = findViewById(R.id.btn_true);

        no = findViewById(R.id.btn_cancle);

        yes.setVisibility(showYes ? View.VISIBLE : View.GONE);

        no.setVisibility(showNo ? View.VISIBLE : View.GONE);

        messageTv = findViewById(R.id.dialogui_tv_title);

    }





    public MyDialog setMessage(String message) {

        messageStr = message;

        return this;

    }



    public interface onYesOnclickListener {

        void onYesClick();

    }



    public interface onNoOnclickListener {

        void onNoClick();

    }



    public static void dialogShow(final Context context, String content, String cancleTxt, String okTxt,final DialogBtnListener dialogBtnListener){

        final MyDialog selfDialog = new MyDialog(context);

        selfDialog.setMessage(""+content);

        selfDialog.setYesOnclickListener(okTxt, new onYesOnclickListener() {

            @Override

            public void onYesClick() {

                dialogBtnListener.yesClick();

            }

        });

        selfDialog.setNoOnclickListener(cancleTxt, new onNoOnclickListener() {

            @Override

            public void onNoClick() {

                dialogBtnListener.noClick();

            }

        });

        selfDialog.show();

    }

    public static void dialogShow(final Context context, String content, String cancleTxt, String okTxt,final YesDialogBtnListener dialogBtnListener){

        final MyDialog selfDialog = new MyDialog(context);

        selfDialog.setMessage(""+content);

        selfDialog.setYesOnclickListener(okTxt, new onYesOnclickListener() {

            @Override

            public void onYesClick() {

                dialogBtnListener.yesClick();

            }

        });

        selfDialog.show();

    }

    @Override

    protected void onStop() {

        super.onStop();

        if (isShowing()){

            dismiss();

        }

    }



    public interface DialogBtnListener {

        void yesClick();

        void noClick();

    }

    public interface YesDialogBtnListener {

        void yesClick();

    }

}


